package com.example.jdbc.pattern.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author ：wuhong-016
 * @version v1.0.0
 * @Create : 2021/1/4 19:21
 * @Description : TODO
 **/
public class CglibProxyImpl implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        this.before();
        return methodProxy.invokeSuper(o, objects);
    }

    private Object target;

    public Object getInstance(Class clasz) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(clasz);
        enhancer.setCallback(this);
        return enhancer.create();
    }

    private void before() {
        System.out.println(">>>>>>> cglib proxy before");
    }
}
